package com.jjx.controller;

import com.github.pagehelper.PageInfo;
import com.jjx.domain.Department;
import com.jjx.domain.Result;
import com.jjx.domain.SysUser;
import com.jjx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("findAll")
    public Result findAll(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page, @RequestParam(name = "limit", required = false, defaultValue = "100") Integer limit, SysUser sysUser) {
        List<SysUser> all = iUserService.findAll(page, limit, sysUser);
        PageInfo<SysUser> pageInfo = new PageInfo<>(all);
        Result rs = new Result();
        rs.setData(pageInfo);
        return rs;
    }

    @RequestMapping("/findAllList")
    @ResponseBody
    public SysUser findAllList() {
        SysUser user = new SysUser();
        SysUser user1 = iUserService.findByCondition(user);
        System.out.println(user1);
        return user1;
    }

    @GetMapping("del")
    public Result del(int[] id) {
        iUserService.delete(id);
        Result rs = new Result();
        return rs;
    }

    @RequestMapping("save")
    public Result save(SysUser sysUser, @RequestParam(name = "upload",required = false) MultipartFile upload, HttpServletRequest request) throws IOException {

        //上传的位置
        String realPath = request.getServletContext().getRealPath("/upload/");

        System.out.println("realPath---" + realPath);

        //判断该路径是否存在
        File file = new File(realPath);
        if (!file.exists()) {
            //不存在 创建文件
            file.mkdirs();
        }

        //说明上传文件项
        //获取上传文件的名称
        String filename = upload.getOriginalFilename();
        //1.PNG
        System.out.println("filename---" + filename);

        //把名字的名称设置唯一值,uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");

        //解决重名覆盖的问题
        filename = uuid + "_" + filename;

        //文件的物理路径
        /*realPath = realPath + filename; //例如 img/jjx.jpg
        System.out.println(realPath);*/


        String sqlPath = realPath.split("OA_war_exploded")[1];
        System.out.println(sqlPath);

        //添加图片路径
        sqlPath = sqlPath + filename;
        sysUser.setImg(sqlPath);

        //持久化到数据库
        Result rs = iUserService.save(sysUser);

        //完成文件的上传
        if (rs.getCode() == 200) {
            upload.transferTo(new File(realPath, filename));
        }


        return rs;
    }

    @RequestMapping("upd")
    public Result upd(SysUser sysUser, @RequestParam(name = "upload",required = false) MultipartFile upload, HttpServletRequest request) throws IOException {
        Result rs = null;
        System.out.println(upload);
        if (upload!=null) {
            //上传的位置
            String realPath = request.getServletContext().getRealPath("/upload/");

            System.out.println("realPath---" + realPath);

            //判断该路径是否存在
            File file = new File(realPath);
            if (!file.exists()) {
                //不存在 创建文件
                file.mkdirs();
            }

            //说明上传文件项
            //获取上传文件的名称
            String filename = upload.getOriginalFilename();
            //1.PNG
            System.out.println("filename---" + filename);

            //把名字的名称设置唯一值,uuid
            String uuid = UUID.randomUUID().toString().replace("-", "");

            //解决重名覆盖的问题
            filename = uuid + "_" + filename;

            //文件的物理路径
        /*realPath = realPath + filename; //例如 img/jjx.jpg
        System.out.println(realPath);*/


            String sqlPath = realPath.split("OA_war_exploded")[1];
            System.out.println(sqlPath);

            //添加图片路径
            sqlPath = sqlPath + filename;
            sysUser.setImg(sqlPath);

            //持久化到数据库
            rs = iUserService.update(sysUser);

            //完成文件的上传
            if (rs.getCode() == 200) {
                upload.transferTo(new File(realPath, filename));
            }
            return rs;
        }
        return iUserService.update(sysUser);
    }
}
